package com.moleq.logic;

import java.util.List;

import com.moleq.enums.ItemType;
import com.moleq.models.ItemDiscountSetting;
import com.moleq.models.GlobalInfo;
import com.moleq.models.PriceSetting;
import com.moleq.models.SaleListItem;
import com.moleq.service.ServiceManager;
import com.moleq.util.BigDecmialTool;
import com.moleq.util.MoleQUtil;

public class PriceManager {

	private static final String LIMITED_QTY_DESC = "Limited Qty";
	private static final String DISCOUNT_AMOUNT_DESC = "Discount %s";
	private static final String DISCOUNT_PERCENT_DESC = "Discount %s";
	private final static String PACK_PRICE_DESC = "Pkg Price Disc";
	private final static String PRICING_PREFIX_DESC = "---";
	public final static int PRICING_MIX_AND_MATCH = 0;
	public final static int PRICING_DISCOUNT_PERCENT = 1;
	public final static int PRICING_DISCOUNT_AMOUNT = 2;
	public final static int PRICING_LIMITED_QTY = 3;
	public final static int PRICING_PACKAGE_PRICE = 4;
	public final static int PRICING_CURRENT_PRICE = 5;
	public final static int PRICING_RETAIL_PRICE = 6;
  
	// public final static int PRICING_MIX_DISCOUNT = 7;

	public int pricingStatus = PRICING_RETAIL_PRICE;

	private int mLimitedQtyCount;
    private boolean isShowEach = false;
	public void process(SaleList saleList, SaleListItem item) {
		PriceSetting priceSetting = item.getPriceSetting();
//		if (!priceSetting.isHaveSetupDate || priceSetting.isHaveSetupDate && priceSetting.isValidDate && !item.getIsWeight()) {
		if (!priceSetting.isHaveSetupDate ||( priceSetting.isHaveSetupDate && priceSetting.isValidDate)) {
			if (item.getIsWeight()) {
				
				if (priceSetting.isDiscountPercent) {
					this.useRetailPrice(saleList, item);
					// mPriceMgr.process(this, item,
					// PriceManager.PRICING_DISCOUNT_PERCENT);
					this.useDsicountPercent(saleList, item);
				}else if(priceSetting.isCurrentPrice){
					this.useCurrentPrice(saleList, item);
				}else{
					this.useRetailPrice(saleList, item);
				}
			}else{
				if (priceSetting.isLimitQty) {

					// item.setItemType(ItemType.REGULAR);
					// addItemToList(item);
					isShowEach =true;
					this.useLimitedQty(saleList, item);

					// mPriceMgr.process(this, item,
					// PriceManager.PRICING_LIMITED_QTY);

				} else if (priceSetting.isMixDiscount && !item.getNodiscount()) {
					item.setItemType(ItemType.DISCOUNT_MIX);
					// saleList.addItemToList(item);
					isShowEach =true;
					this.useRetailPrice(saleList, item);

					if (priceSetting.isDiscountAmount) {
						// mPriceMgr.process(this, item,
						// PriceManager.PRICING_DISCOUNT_AMOUNT);
						this.useDiscountAmount(saleList, item);

					}
					if (priceSetting.isDiscountPercent) {
						// mPriceMgr.process(this, item,
						// PriceManager.PRICING_DISCOUNT_PERCENT);
						this.useDsicountPercent(saleList, item);
					}

					if (priceSetting.isDiscountLink) {
						this.useDsicountLink(saleList, item);
					}
				} else if (priceSetting.isPackagePrice) {

					// item.setItemType(ItemType.PACKAGE_PRICE);
					// addItemToList(item);

					// mPriceMgr.process(this, item,
					// PriceManager.PRICING_PACKAGE_PRICE);
					if (item.getIsWeight()) {
						this.useRetailPrice(saleList, item);
					} else {
						
						this.usePackagePirce(saleList, item);
					}

				} else if (priceSetting.isCurrentPrice) {
					// mPriceMgr.process(this, item,
					// PriceManager.PRICING_CURRENT_PRICE);
					this.useCurrentPrice(saleList, item);

				} else {
					// mPriceMgr.process(this, item,
					// PriceManager.PRICING_RETAIL_PRICE);
					isShowEach =true;
					this.useRetailPrice(saleList, item);

				}
			}
			
		} else {
			// mPriceMgr.process(this, item, PriceManager.PRICING_RETAIL_PRICE);
			isShowEach =true;
			this.useRetailPrice(saleList, item);

		}
	}

	private void useRetailPrice(SaleList list, SaleListItem item) {

		SaleListItem result = item;
		int totalQtyInList = list.getQtyByItem(item);
		int curentItemQty = item.getQty();
		PriceSetting setting = item.getPriceSetting();
		String currentWeightDesc = item.getWeightDesc();

		if (setting.isMixAndMatch) {
			List<SaleListItem> items = ServiceManager.getInstance().mMixMatchService.findItemsByNo(setting.getPricemm());
			totalQtyInList = list.getQtyByItemNoWeight(items);
		}

		int mRetailQty = setting.mRetailQty;
		double mRetailPrice = setting.mRetailPrice;
		int mLimitQty = setting.mLimitQty;

		int k, j, i;
		double totalP4 = 0;
		double unitP4 = 0;
		double tempPrice = 0;
		if (item.getIsWeight()) {
//			unitP4 = BigDecmialTool.div(mRetailPrice, mRetailQty);
			unitP4 = mRetailPrice / mRetailQty;
			totalP4 = unitP4 * item.getWeight();
			totalP4 = BigDecmialTool.round(totalP4, 2);
		} else {
			k = totalQtyInList + curentItemQty - mLimitQty;
			// i = k - curentItemQty;
			i = totalQtyInList - mLimitQty;
			for (; i < k; i++) {
				j = i % mRetailQty;
				tempPrice = calcPrice3(mRetailPrice, mRetailQty, j);
				result.setVdrCouponPrice(tempPrice);
				totalP4 += tempPrice;
			}

		}

		String frontDesc = "";
		if (currentWeightDesc.equals("")) {
			frontDesc = String.valueOf(curentItemQty);
		} else {
			// currentWeightDesc += " @ "+ unitP4 +"/"+
			// TransactionManager.getInstance().getPosSetting().getPs_weight_unit();
			//fixed receipt
//			currentWeightDesc += " @ " + GlobalInfo.getInstance().CurrencySign + MoleQUtil.fixNumber(unitP4) + "/" + GlobalInfo.getInstance().weightUnit;

//			currentWeightDesc += result.getmTareDesc();

			frontDesc = currentWeightDesc;
		}

		if (mRetailQty > 1) {
			if (result.getIsWeight()) {
				result.setPriceDescription(frontDesc + " @ " + String.valueOf(mRetailQty) + " " + GlobalInfo.getInstance().weightUnit + "/" + GlobalInfo.getInstance().CurrencySign + MoleQUtil.fixNumber(BigDecmialTool.round(mRetailPrice, 2))  + item.getMixMatchDesc());
			}else {
				result.setPriceDescription(frontDesc + " @ " + String.valueOf(mRetailQty) + " FOR " + GlobalInfo.getInstance().CurrencySign + MoleQUtil.fixNumber(BigDecmialTool.round(mRetailPrice, 2))  + item.getMixMatchDesc());
			}
		} else if (!currentWeightDesc.equals("")) {
			currentWeightDesc += " @ " + GlobalInfo.getInstance().CurrencySign + MoleQUtil.fixNumber(BigDecmialTool.round(item.getUnitPrice(),2)) +"/"+ GlobalInfo.getInstance().weightUnit;
			result.setPriceDescription(currentWeightDesc);
		}else{
			if (item.getQty() > 1 && isShowEach) {
//			 result.setPriceDescription(MoleQUtil.fixPriceForScreen(item.getUnitPrice())+" EACH");
				result.setEachDescription(item.getQty() + " @ 1 / "+MoleQUtil.fixPriceForScreen(item.getUnitPrice()));
			 
			}
		}
		isShowEach = false;
		//set RetailPrice
//		setting.mRetailPrice = totalP4;
		
		result.setPrice(totalP4);
		result.setItemType(ItemType.REGULAR);

		list.addItemToList(result);
	}

	private double calcPrice(double pirce, int qty, int cnt) {

		double totalP4, remainP4, avgP4, overP4, x;
		remainP4 = pirce;
		int k = qty;
		totalP4 = pirce;

		for (int i = 0; i < qty; i++) {

			avgP4 = BigDecmialTool.div(remainP4, k, false);
			overP4 = BigDecmialTool.overDecimal(remainP4, k);
			x = BigDecmialTool.overDecimalnoRoundUp(remainP4, k);

			if (overP4 > 0) {
				totalP4 = avgP4 + overP4;
			} else {
				if (k - 1 == 0) {
					totalP4 = avgP4;
				} else {
					totalP4 = avgP4 + x / (k - 1);
				}
			}

			if (i == cnt) {
				break;
			}
			k--;
			remainP4 = BigDecmialTool.sub(remainP4, totalP4);
		}

		return totalP4;
	}

	private double calcPrice3(double pirce, int qty, int cnt) {

		double totalP4, remainP4, avgP4, overP4, x;
		remainP4 = pirce;
		int k = qty;
		totalP4 = pirce;

		avgP4 = BigDecmialTool.div(remainP4, k);
		overP4 = BigDecmialTool.overDecimal(remainP4, k);
		// x = BigDecmialTool.overDecimalnoRoundUp(remainP4,k);

		if (overP4 > 0) {
			if (cnt == 0) {
				totalP4 = avgP4 + overP4;
			} else {
				totalP4 = avgP4;
			}

		} else {
			if (k - 1 == cnt) {
				totalP4 = avgP4 + overP4;
			} else {
				totalP4 = avgP4;
			}
		}

		return totalP4;
	}

	private void useCurrentPrice(SaleList list, SaleListItem item) {

		SaleListItem result = item;
		int totalQtyInList = list.getQtyByItem(item);
		int curentItemQty = item.getQty();
		String currentWeightDesc = item.getWeightDesc();

		int k, j, i, k2, k3;
		double totalP4, avgP4, overP4;
		PriceSetting setting = item.getPriceSetting();

		if (setting.isMixAndMatch) {
			List<SaleListItem> items = ServiceManager.getInstance().mMixMatchService.findItemsByNo(setting.getPricemm());
			totalQtyInList = list.getQtyByItemNoWeight(items);
		}

		int mCurrentPriceQty = setting.mCurrentPriceQty;
		double mCurrentPrice = setting.mCurrentPrice;
		int mLimitQty = setting.mLimitQty;

		totalP4 = 0;
		avgP4 = 0;
		overP4 = 0;
		double tempPrice = 0;
		if (item.getIsWeight()) {
			totalP4 = BigDecmialTool.div(mCurrentPrice, mCurrentPriceQty) * item.getWeight();
		} else {
			k = totalQtyInList + curentItemQty - mLimitQty;
			// i = k - curentItemQty;
			i = totalQtyInList - mLimitQty;

			for (; i < k; i++) {
				j = i % mCurrentPriceQty;
				tempPrice = calcPrice3(mCurrentPrice, mCurrentPriceQty, j);
				result.setVdrCouponPrice(tempPrice);
				totalP4 += tempPrice;
			}

		}

		result.setPrice(totalP4);
		String frontDesc = "";
		if (currentWeightDesc.equals("")) {
			frontDesc = String.valueOf(curentItemQty);
		} else {
			frontDesc = currentWeightDesc;
		}
		result.setPriceDescription(frontDesc + " @ " + String.valueOf(mCurrentPriceQty) + "/" + GlobalInfo.getInstance().CurrencySign + MoleQUtil.fixNumber(BigDecmialTool.round(mCurrentPrice, 2)) + item.getMixMatchDesc());

		result.setItemType(ItemType.MULTIPLE_PRICE);

		list.addItemToList(result);

	}

	// package price
	private void usePackagePirce(SaleList list, SaleListItem item) {
		// TODO Auto-generated method stub
		int k, j, i, k2, k3;
		double totalP4, avgP4, overP4, remainPrice;
		boolean haveUserMM = false;
		PriceSetting setting = item.getPriceSetting();
		String currentWeightDesc = item.getWeightDesc();
		int totalQtyInList = 0;
		if (item.isRefund()) {
			totalQtyInList = list.getQtyByItemRefund(item);
		}else{
			totalQtyInList = list.getQtyByItem(item);
		}
		
		int curentItemQty = item.getQty();
		double totalPackagePirce = 0,totalMMPrice = 0;
		totalP4 = 0;

		int mPackageQty = setting.mPackageQty;
		double mPackagePrice = setting.mPackagePrice;
		double mRetailPrice = setting.mRetailPrice;
		List<SaleListItem> items = null;
		if (setting.isMixAndMatch && list.isUseMM(item)) {
			haveUserMM = true;
			 items = ServiceManager.getInstance().mMixMatchService.findItemsByNo(setting.getPricemm());
			totalQtyInList = list.getQtyByItemNoWeight(items);
			totalMMPrice = list.getMMPriceByItem(item.getMixMatch(),mPackagePrice);
			int pqty = (totalQtyInList + curentItemQty) % mPackageQty;
			int qty = 0;
			if (curentItemQty != 0) {	
				qty = item.getQty() - pqty;	
			}else{
				qty = 0;
			}
			totalPackagePirce = list.geTotalByItemNoWeight(items) + mRetailPrice * qty + totalMMPrice;
		}else{
			totalPackagePirce = mPackageQty * mRetailPrice;
		}

		if (!setting.isLimitQty) {
			mLimitedQtyCount = 0;
		}

		String frontDesc = "";
		if (currentWeightDesc.equals("")) {
			frontDesc = String.valueOf(curentItemQty);
		} else {
			frontDesc = currentWeightDesc;
		}
		item.setPriceDescription(frontDesc + " @ " + String.valueOf(mPackageQty) + " / " + GlobalInfo.getInstance().CurrencySign + MoleQUtil.fixNumber(mPackagePrice) + item.getMixMatchDesc());
		item.setItemType(ItemType.PACKAGE_PRICE);
		item.setPrice(mRetailPrice * item.getQty());
		item.setVdrCouponPrice(mRetailPrice);
		list.addItemToList(item);

		// totalQtyInList = totalQtyInList - curentItemQty;

		totalQtyInList = totalQtyInList - mLimitedQtyCount;
		k = totalQtyInList + curentItemQty;
		i = k - curentItemQty;
		
		j = -1;
		for (; i < k; i++) {

			j = (i + 1) % mPackageQty;
			// j = (i + 1 - mLimitedQtyCount)% mPackageQty;

			if (j == 0) {

//				if (setting.isMixAndMatch) {
//					totalP4 += mPackagePrice - mRetailPrice * mPackageQty;
//				}else{
					totalP4 += mPackagePrice -(totalPackagePirce);
//				}
//				totalP4 += mPackagePrice - mRetailPrice * mPackageQty;
//				totalP4 += mPackagePrice -(totalPackagePirce);
			}
		}
		if (totalP4 != 0 ) {
			if (haveUserMM) {
				list.setPriceQtyByMM(items, mPackageQty);
			}else{
				list.setPriceQty(item, mPackageQty);
			}
			
			SaleListItem result = list.createItem();
			item.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);
			result.setParentId(item.getId());
			result.setTax1(item.getTax1());
			result.setTax2(item.getTax2());
			result.setTax3(item.getTax3());
			result.setTax4(item.getTax4());
			result.setTax5(item.getTax5());
			result.setTax6(item.getTax6());
			result.setFoodstamp(item.getFoodstamp());
			result.setNodiscount(item.getNodiscount());
			result.setItemType(ItemType.PACKAGE_PRICE_FIX);
			result.setUpc("PKGPrice");
			result.setName( PACK_PRICE_DESC);
			result.setDisplayContent(PRICING_PREFIX_DESC + PACK_PRICE_DESC);
			result.setWic(item.isWic());
			result.setCvv(item.isCvv());
			if (setting.isMixAndMatch) {
				result.setName("MM " + result.getName());
				result.setDisplayContent("MM " + result.getDisplayContent());
//				result.setMixMatch(item.getMixMatch());
			}
			result.setMixMatch(item.getMixMatch());
			result.setRefund(item.isRefund());
			result.setQty(1);

			result.setPrice(totalP4);
			
			result.setItemType(ItemType.PACKAGE_PRICE_FIX);
			list.addItemToList(result);

		} else {
			// result = null;
		}

		// if(result!=null){
		// result.setItemType(ItemType.PACKAGE_PRICE_FIX);
		// list.addItemToList(result);
		// }
	}

	// limited qty
	private void useLimitedQty(SaleList list, SaleListItem item) {

		double subP4 = 0;
		int totalQtyInList = list.getQtyByItem(item);
		int curentItemQty = item.getQty();
		int i, j;
		PriceSetting setting = item.getPriceSetting();

		if (setting.isMixAndMatch) {
			List<SaleListItem> items = ServiceManager.getInstance().mMixMatchService.findItemsByNo(setting.getPricemm());
			totalQtyInList = list.getQtyByItemNoWeight(items);
		}

		mLimitedQtyCount = 0;
		// useRetailPrice(list, item);

		// totalQtyInList = totalQtyInList - curentItemQty;
		i = totalQtyInList - setting.mLimitQty;

		if (i >= 0) {

			// isOutOfLimitQty = true;
			// useRetailPrice(list, item);

			mLimitedQtyCount = setting.mLimitQty;

			if (setting.isMixDiscount && !item.getNodiscount()) {

				list.addItemToList(item);

				if (setting.isDiscountAmount) {
					this.process(list, item, PriceManager.PRICING_DISCOUNT_AMOUNT);
				}
				if (setting.isDiscountPercent) {
					process(list, item, PriceManager.PRICING_DISCOUNT_PERCENT);
				}
			} else if (setting.isPackagePrice && !item.getIsWeight()) {
				process(list, item, PriceManager.PRICING_PACKAGE_PRICE);
			} else if (setting.isCurrentPrice) {
				process(list, item, PriceManager.PRICING_CURRENT_PRICE);
			} else {
				process(list, item, PriceManager.PRICING_RETAIL_PRICE);

			}

		} else {
			
			useRetailPrice(list, item);
			
			j = curentItemQty - Math.abs(i);

			if (j > 0) {
				mLimitedQtyCount = Math.abs(i);
				subP4 = BigDecmialTool.mul(Math.abs(i), setting.mRetailPrice - setting.mLimitPrice);
			} else {
				mLimitedQtyCount = curentItemQty;
				subP4 = BigDecmialTool.mul(curentItemQty, setting.mRetailPrice - setting.mLimitPrice);
			}

			subP4 = -1 * subP4;

			SaleListItem result = list.createItem();
			list.setLimitPriceQty(item);
			item.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);
			result.setParentId(item.getId());
			result.setQty(1);
			result.setTax1(item.getTax1());
			result.setTax2(item.getTax2());
			result.setTax3(item.getTax3());
			result.setTax4(item.getTax4());
			result.setTax5(item.getTax5());
			result.setTax6(item.getTax6());
			result.setFoodstamp(item.getFoodstamp());
			result.setNodiscount(item.getNodiscount());
			result.setUpc("LimitQty");
			result.setName( LIMITED_QTY_DESC);
			result.setDisplayContent(PRICING_PREFIX_DESC + LIMITED_QTY_DESC);
			result.setCvv(item.isCvv());
			result.setWic(item.isWic());
			
			if (setting.isMixAndMatch) {
				result.setName("MM " + result.getName());
				result.setDisplayContent("MM " + result.getDisplayContent());
			}
			result.setRefund(item.isRefund());
			result.setPrice(subP4);
			result.setItemType(ItemType.LIMITED_QTY_PRICE_FIX);
			list.addItemToList(result);
		}
	}

	// Discount Amount
	private void useDiscountAmount(SaleList saleList, SaleListItem item) {
		PriceSetting setting = item.getPriceSetting();
		SaleListItem discntAmtItem = saleList.createItem();
		item.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);
		discntAmtItem.setParentId(item.getId());
		discntAmtItem.setTax1(item.getTax1());
		discntAmtItem.setTax2(item.getTax2());
		discntAmtItem.setTax3(item.getTax3());
		discntAmtItem.setTax4(item.getTax4());
		discntAmtItem.setTax5(item.getTax5());
		discntAmtItem.setTax6(item.getTax6());
		discntAmtItem.setFoodstamp(item.getFoodstamp());
		discntAmtItem.setNodiscount(item.getNodiscount());
		discntAmtItem.setItemType(ItemType.DISCOUNT_AMOUNT);	
		discntAmtItem.setUpc("DISCOUNTAMOUNT");
		String sDesc = String.format(DISCOUNT_AMOUNT_DESC, MoleQUtil.fixNumberIncludeCurrencySign(setting.mDiscountAmount));
		discntAmtItem.setName( sDesc);
		discntAmtItem.setDisplayContent(PRICING_PREFIX_DESC + sDesc);
		discntAmtItem.setCvv(item.isCvv());
		discntAmtItem.setWic(item.isWic());
		if (setting.isMixAndMatch) {
			discntAmtItem.setName("MM " + discntAmtItem.getName());
			discntAmtItem.setDisplayContent("MM " + discntAmtItem.getDisplayContent());
		}

		discntAmtItem.setRefund(item.isRefund());
		discntAmtItem.setQty(1);
		double discuntAmt = BigDecmialTool.abs(setting.mDiscountAmount);
		discuntAmt = BigDecmialTool.mul(item.getQty(), setting.mDiscountAmount);
		discuntAmt = BigDecmialTool.mul(-1, discuntAmt);
		discntAmtItem.setPrice(discuntAmt);
		if (item.getVdrCouponPrice() > 0) {
			item.setVdrCouponPrice(item.getVdrCouponPrice() - setting.mDiscountAmount);
		}else{
			item.setVdrCouponPrice(item.getPrice() + setting.mDiscountAmount);
		}
		
		saleList.addItemToList(discntAmtItem);

	}

	// Discount Percent
	private void useDsicountPercent(SaleList saleList, SaleListItem item) {
		PriceSetting setting = item.getPriceSetting();
		SaleListItem discntPerItem = saleList.createItem();
		item.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);
		discntPerItem.setParentId(item.getId());
		discntPerItem.setTax1(item.getTax1());
		discntPerItem.setTax2(item.getTax2());
		discntPerItem.setTax3(item.getTax3());
		discntPerItem.setTax4(item.getTax4());
		discntPerItem.setTax5(item.getTax5());
		discntPerItem.setTax6(item.getTax6());
		discntPerItem.setFoodstamp(item.getFoodstamp());
		discntPerItem.setNodiscount(item.getNodiscount());
		discntPerItem.setItemType(ItemType.DISCOUNT_PERECENT);
		discntPerItem.setUpc("DiscountRate");
		String sDesc = String.format(DISCOUNT_PERCENT_DESC, MoleQUtil.fixNumber2(setting.mDiscountPercent * 100)) + "%";
		discntPerItem.setName( sDesc);
		discntPerItem.setDisplayContent(PRICING_PREFIX_DESC + sDesc);
		discntPerItem.setCvv(item.isCvv());
		discntPerItem.setWic(item.isWic());
		if (setting.isMixAndMatch) {
			discntPerItem.setName("MM " + discntPerItem.getName());
			discntPerItem.setDisplayContent("MM " + discntPerItem.getDisplayContent());
		}

		discntPerItem.setRefund(item.isRefund());
		discntPerItem.setQty(1);
		double discuntAmt = BigDecmialTool.mul(item.getPrice(), setting.mDiscountPercent);
		if(discuntAmt > 0)discuntAmt = BigDecmialTool.mul(-1, discuntAmt);
		discntPerItem.setPrice(discuntAmt);
		double tempVdrPrice = BigDecmialTool.mul(item.getUnitPrice(), setting.mDiscountPercent);
		if (item.getVdrCouponPrice() > 0) {
			item.setVdrCouponPrice(item.getVdrCouponPrice() - tempVdrPrice);
		}else{
			item.setVdrCouponPrice(item.getPrice() - tempVdrPrice);
		}
		saleList.addItemToList(discntPerItem);
	}

	private void useDsicountLink(SaleList saleList, SaleListItem item) {
		PriceSetting setting = item.getPriceSetting();
		String dislink = setting.getDislink();
		ItemDiscountSetting discnt = null;
		if (dislink != null && !dislink.trim().equals("")) {
			discnt = ServiceManager.getInstance().mItemDiscountSettingService.Find(Integer.valueOf(dislink));
		}
		
		ItemDiscountSetting ds = new ItemDiscountSetting();
		ds = ServiceManager.getInstance().mDiscountSettingService.Find(Integer.valueOf(setting.getDislink().toString()));
		if (ds == null) {
			return;
		}
		SaleListItem discntLinkItem = saleList.createItem();
		item.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);
		discntLinkItem.setParentId(item.getId());
		if (discnt != null && !discnt.isDs_follow_item()) {
			discntLinkItem.setTax1(discnt.isDs_tax1());
			discntLinkItem.setTax2(discnt.isDs_tax2());
			discntLinkItem.setTax3(discnt.isDs_tax3());
			discntLinkItem.setTax4(discnt.isDs_tax4());
			discntLinkItem.setTax5(discnt.isDs_tax5());
			discntLinkItem.setTax6(discnt.isDs_tax6());
			discntLinkItem.setFoodstamp(discnt.isDs_foodstamp());
		}else{
			discntLinkItem.setTax1(item.getTax1());
			discntLinkItem.setTax2(item.getTax2());
			discntLinkItem.setTax3(item.getTax3());
			discntLinkItem.setTax4(item.getTax4());
			discntLinkItem.setTax5(item.getTax5());
			discntLinkItem.setTax6(item.getTax6());
			discntLinkItem.setFoodstamp(item.getFoodstamp());
		}
		discntLinkItem.setNodiscount(item.getNodiscount());
		discntLinkItem.setItemType(ItemType.DISCOUNT_LINK);
		discntLinkItem.setUpc("DISCOUNT_LINK");
		discntLinkItem.setPoslinediscntno(ds.getDs_no());
		String sDesc = ds.getDs_desc() ;
		discntLinkItem.setName( sDesc);
		discntLinkItem.setDisplayContent(PRICING_PREFIX_DESC + sDesc);
		discntLinkItem.setWic(item.isWic());
		discntLinkItem.setCvv(item.isCvv());
		
		if (setting.isMixAndMatch) {
			discntLinkItem.setName("MM " + discntLinkItem.getName());
			discntLinkItem.setDisplayContent("MM " + discntLinkItem.getDisplayContent());
		}

		discntLinkItem.setRefund(item.isRefund());
		discntLinkItem.setQty(1);
		double discuntAmt = 0,tempVdrPrice = 0;
		if (ds.getDs_type().equals("P")) {
			discuntAmt = BigDecmialTool.mul(item.getPrice(), ds.getDs_value() * 0.01);
			tempVdrPrice = BigDecmialTool.mul(item.getUnitPrice(), ds.getDs_value() * 0.01);
		}else{
			discuntAmt = BigDecmialTool.mul(ds.getDs_value(),item.getQty()) ;
			tempVdrPrice = ds.getDs_value();
		}
		
		if(discuntAmt > 0)discuntAmt = BigDecmialTool.mul(-1, discuntAmt);
		discntLinkItem.setPrice(discuntAmt);
		if (item.getVdrCouponPrice() > 0) {
			item.setVdrCouponPrice(item.getVdrCouponPrice() - tempVdrPrice);
		}else{
			item.setVdrCouponPrice(item.getPrice() - tempVdrPrice);
		}
		saleList.addItemToList(discntLinkItem);
	}

	private SaleListItem useMixAndMatch(SaleList saleList, SaleListItem item) {
		// TODO Auto-generated method stub
		return null;
	}

	public void process(SaleList saleList, SaleListItem item, int status) {
		SaleListItem result = item;

		if (result == null)
			return;

		switch (status) {

		case PRICING_MIX_AND_MATCH:
			useMixAndMatch(saleList, item);
			break;

		case PRICING_DISCOUNT_PERCENT:

			useDsicountPercent(saleList, item);
			break;

		case PRICING_DISCOUNT_AMOUNT:
			useDiscountAmount(saleList, item);
			break;

		case PRICING_LIMITED_QTY:
			useLimitedQty(saleList, item);
			break;

		case PRICING_PACKAGE_PRICE:
			usePackagePirce(saleList, item);
			break;

		case PRICING_CURRENT_PRICE:
			useCurrentPrice(saleList, item);
			break;

		case PRICING_RETAIL_PRICE:
			useRetailPrice(saleList, item);
			break;

		}

	}

}
